class Solution(object):
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        # i = 0
        # while i < len(flowerbed) + 1:
        #     if flowerbed[i] == 0 and flowerbed[i + 1] == 0:
        #         flowerbed[i+1] = 1
        #         n -= 1
        #     i += 1
        #     if n <= 0:
        #         return True
        # return False

        flowerbed = [0] + flowerbed + [0]
        for i in range(1, len(flowerbed) -1):
            if flowerbed[i] == 0 and flowerbed[i - 1] == 0 and flowerbed[i + 1] == 0:
                flowerbed[i] = 1
                n -= 1
            if n <= 0:
                return True
        return False



if __name__ == '__main__':
    print(Solution().canPlaceFlowers([0, 0, 1, 0, 1], 1))
